package com.yuchen.ishop.web.app.interceptor;

import com.yuchen.ishop.service.goods.GoodsTemplateRestClient;
import com.yuchen.ishop.service.seller.SellerRestClient;
import com.yuchen.ishop.service.seller.XiaoerRestClient;
import com.yuchen.ishop.vo.CookieUserInfo;
import com.yuchen.ishop.web.BaseAction;
import com.yuchen.jaxrs.Responses;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author Jack Q
 * buyer,seller,xiaoer 用户中心拦截
 */
public class MySellerInterceptor implements HandlerInterceptor {

    private final static Logger LOG = Logger.getLogger(MySellerInterceptor.class);

    @Autowired
    private SellerRestClient sellerRestClient;

    @Autowired
    private XiaoerRestClient xiaoerRestClient;

    @Autowired
    private GoodsTemplateRestClient goodsTemplateRestClient;


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        LOG.debug(request.getServletPath());
        String url = request.getServletPath();
        CookieUserInfo userInfo = BaseAction.cookie2UserInfo(request);
        if (userInfo == null || !userInfo.isState() || "buyer".equals(userInfo.getRole())) {
            response.sendRedirect(request.getContextPath() + "/goLogin.htm");
            return false;
        }
        Long userId =userInfo.getId() ;
        if (url.startsWith("/my/seller/deleteXiaoer.htm") || url.startsWith("/my/seller/editXiaoer.htm") ) {
            Long id = Long.valueOf(request.getParameter("id"));
            Responses responses =  xiaoerRestClient.existXiaoer(userId,id);
            LOG.info(responses);
            if(!responses.isStatus()){
                response.sendRedirect(request.getContextPath() + "/error.htm");
                return false;
            }
        }
        //商品安全拦截
        if (url.startsWith("/my/seller/deleteGoods.htm") || url.startsWith("/my/seller/editGoods.htm")
                || url.startsWith("/my/seller/offGoods.htm")) {
            Long id = Long.valueOf(request.getParameter("id"));
            String role = userInfo.getRole();
            Responses responses =  sellerRestClient.existGoods(userId,id,role);
            LOG.info(responses);
            if(!responses.isStatus()){
                response.sendRedirect(request.getContextPath() + "/error.htm");
                return false;
            }
        }
        //拦截模版导入
        if(url.startsWith("/my/seller/importGoods.htm") || url.startsWith("/my/seller/deleteGoodsTemplate.htm") ){
            Long templateId = Long.valueOf(request.getParameter("templateId"));
            if(templateId==null || !goodsTemplateRestClient.isExistTemplate(templateId,userInfo.getId())){
                response.sendRedirect(request.getContextPath() + "/error.htm");
                return false;
            }

        }


        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }


}
